package com.itheima.api.treeset.demo02;

import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetDemo01 {

    public static void main(String[] args) {

        Student s1 = new Student("刘德华", 23);
        Student s2 = new Student("古天乐", 18);
        Student s3 = new Student("张家辉", 20);
        Student s4 = new Student("吴镇宇", 20);

//        TreeSet<Student> treeSet = new TreeSet<Student>(new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                int result = o1.getAge() - o2.getAge();
//
//                int result2 = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
//
//                return result2;
//            }
//        });

        TreeSet<Student> treeSet = new TreeSet<Student>((o1, o2) -> {
            int result = o1.getAge() - o2.getAge();
            int result2 = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
            return result2;
        });

        treeSet.add(s1);
        treeSet.add(s2);
        treeSet.add(s3);
        treeSet.add(s4);

        treeSet.forEach(s -> System.out.println(s));

    }

}
